package com.gopay.gass.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.gass.message.GassBcdlNotifyRespMessage;
import com.gopay.gass.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.gass.domain.GassMainOrder;
import com.gopay.gass.exception.GassServiceException;
import com.gopay.gass.message.GassPayReqMessage;
import com.gopay.gass.message.GassPayRespMessage;
import com.gopay.gass.message.ResultCode;

/**
 * Created by HJY on 2015/3/20.
 */
@Service("gassPayService")
public  class GassPayServiceImpl implements GassPayService {

    @Resource(name = "payRequestService")
    private Map<String,GassPayRequestService> payRequestService;

    @Resource(name = "payQueryService")
    private Map<String,GassOrderQueryService> payQueryService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource(name = "gassOrderService")
    private GassOrderService gassOrderService;

    @Resource(name = "gassPayBankSelectorService")
    private GassPayBankSelectorService gassPayBankSelectorService;

    @Resource(name = "gassBcdlNotifyService")
    private  GassBcdlNotifyService gassBcdlNotifyService;

    @Override
    public GassPayRespMessage pay(GassPayReqMessage reqMsg) {
        GassPayRespMessage respMsg = null;
        String payBankCode = reqMsg.getPayBankCode();
        String stlmEx = reqMsg.getStlmEx();
        try{
        	logger.info("Pay Request stlmEx: "+reqMsg.getStlmEx());
        	/**
        	 * modified by zyt, 2017-2-22，修改输出日志中的全卡号做掩码显示，json的输出格式改为tostring的格式
        	 */
            logger.info("Pay Request Info: "+LoggerMaskUtils.maskString(reqMsg.toString()));
            GassMainOrder order = gassOrderService.getMainOrder(reqMsg.getGopayOrderId(), reqMsg.getOrderSource());
            if(order != null && !(order.getStlmStatus() == ResultCode.NO.getValue() || order.getStlmStatus() == ResultCode.FAILED.getValue())){
                logger.error("Pay Response Info: gopayOrderId="+order.getGopayOrderId()+" has payed! ");
                //重复付款
                respMsg = new GassPayRespMessage();
                respMsg.setResponseId(order.getGassId());
                ResultCode resultCode = ResultCode.get(order.getStlmStatus());

                respMsg.setResultCode(resultCode);
                respMsg.setResultMsg(order.getStlmDes());
                payBankCode = order.getPayBankCode();
            }else{
                //根据收款银行定位付款银行
//                if(StringUtils.isEmpty(payBankCode))
//                    payBankCode = gassPayBankSelectorService.getPayBank(reqMsg.getRecvBankName());

//                reqMsg.setPayBankCode(payBankCode);
                GassPayRequestService payReq = payRequestService.get(stlmEx);
                if(payReq == null)
                    throw new GassServiceException(String.format("付款银行 %s 不支持 ",stlmEx));

                respMsg = payReq.processReq(reqMsg);
            }
        }catch (Exception e){
            respMsg = new GassPayRespMessage();
            respMsg.setResultCode(ResultCode.FAILED);

            if(e instanceof GassServiceException){
                respMsg.setResultMsg(((GassServiceException)e).getErrorMsg());
            }else{
                respMsg.setResultMsg(e.getMessage());
            }
        }
        respMsg.setPayBankCode(payBankCode);
        return respMsg;
    }
    @Override
    public GassPayRespMessage query(String gopayOrderId,String orderSource) {
        logger.info(String.format("Query Request Info gopayOrderId:%s ,orderSource;%s ",gopayOrderId,orderSource));
        GassMainOrder order = gassOrderService.getMainOrder(gopayOrderId, orderSource);
        GassPayRespMessage respMessage = null;
        if(order != null){
            if(order.getStlmStatus() == ResultCode.SUCCESS.getValue()){
                respMessage = new GassPayRespMessage();
                respMessage.setResponseId(order.getGassId());
                respMessage.setResultMsg(order.getStlmDes());
                respMessage.setResultCode(ResultCode.SUCCESS);
            }else{
                GassOrderQueryService queryService = payQueryService.get(order.getPayBankCode());
                respMessage = queryService.query(order);
            }
        }
        return respMessage;
    }

    @Override
    public GassBcdlNotifyRespMessage bcdlNotifyGass(Map<String, String> param) {
       return gassBcdlNotifyService.processing(param);
    }
}
